home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1237 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: returning a string from a function
  5. Date: 12 Jan 1996 07:43 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <12JAN199607430522@erich.triumf.ca>
  9. References: <4d4uh8$q46@mailhost.mwmicro.com>
  10. NNTP-Posting-Host: erich.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <4d4uh8$q46@mailhost.mwmicro.com>, aschlies@citynet.net writes...
  14. >I am learning C with the aid of only a book.  I am at an inpass in
  15. >this process.  I have a small function that needs to return a string
  16. >back to the main routine. I have the following prototype:
  17. >char function_name(char in_string[80])
  18.  
  19. To return a string, this must be char *function(...)
  20. >{
  21. >    char value[80];
  22. >    ...value takes on part of the value of the last 10 characters
  23. >      of in_string.
  24. >    return(value);
  25.  
  26. SERIOUS error! - you are returning a pointer to an automatic variable, which
  27. becomes invalid when your function terminates. Change "char value[80]" to
  28. "static char value[80]" to ensure that "value" remains valid after the function
  29. terminates.
  30.  
  31. >Also, I wrote the routine that copies the last 10 characters. Is there
  32. >a lib. function that does that for me?? Did I reinvent the wheel?
  33.  
  34. There is a strncpy() that copies n chars - it could be used in a function to
  35. return the last n...
  36.  
  37. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  38. Internet: bennett@triumf.ca         | of one another only when one can be
  39. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  40. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  41. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  42.  
  43.  
  44.  
  45.  
  46.  
  47.